package stdio.costa.moca.model;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.persistence.PostPersist;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

import stdio.costa.UuidPersistentable;

@Entity(name = "User")
@NamedQueries({ @NamedQuery(name = "findUserByName", query = "from User u where u.name =:name") })
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class User extends UuidPersistentable {

	private static final long serialVersionUID = -6395532877541011594L;

	@Column(name = "name", unique = true)
	private String name;

	@XmlTransient
	@Column(name = "password")
	private String password;
	
	@OneToOne(cascade = CascadeType.ALL)
	private Address address;

	public User() {
		this(null, null);
	}

	public User(String name, String password) {
		this(name, password, new Address());
	}
	
	public User(String name, String password, Address address) {
		this.name = name;
		this.password = password;
		this.address = address;
	}

	@PostPersist
	protected void onPostPersist() {
		setCreatedBy(getUuid());
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Address getAddress() {
		return address;
	}

	public void setAddress(Address address) {
		this.address = address;
	}
}
